package com.ruoyi.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CalcUtil {
    public static BigDecimal multiplyNum = new BigDecimal(100);
    public static BigDecimal zero = new BigDecimal(0);

    public static String calcRate(BigDecimal fenzi, BigDecimal fenmu) {
        if (fenmu.equals(zero)) {
            return "--";
        }
        BigDecimal result = fenzi.multiply(multiplyNum).divide(fenmu, 2, RoundingMode.HALF_UP);
        return result + "%";
    }

    //计算百分乘法
    public static BigDecimal calcPercentage(BigDecimal source, String percentage) {
        if (null == source) {
            return null;
        }
        if (StringUtils.isEmpty(percentage)) {
            return null;
        }
        percentage = percentage.replace("%","");
        Float f = Float.valueOf(percentage) / 100;
        BigDecimal decimal = new BigDecimal(f);
        decimal = decimal.setScale(2, BigDecimal.ROUND_HALF_UP);
        return source.multiply(decimal).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

}
